<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\InInvoices as InvoicesModel;
use think\Db;
use app\common\Logs;
class InInvoices extends BaseController{    
    public function __construct(InvoicesModel $invoicesModel){
        parent::__construct();
        $this->model = $invoicesModel;
    }
    public function index(){
        $params = $this->request->param();
        $where['income_id']=$params['income_id'];
        $where['inout'] = $params['inout'];
        $where['type'] = $params['type'];
        $data = $this->model->allowField(true)->where($where)->select();        
        return $data;
    } 
    public function search(){
        $params = $this->request->param();
        $where=[];
        $xwhere=[];
        $page = 1;
        $pageCount = 10;
        if(!empty($params)){      
            //按开票日期查询
            if(!empty($params['start_date']) && !empty($params['end_date'])){
               $where['invoice_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];            
            }
           //页码            
           $page = !empty($params['page']) ? $params['page'] : $page;
           $pageCount = !empty($params['page_count']) ? $params['page_count'] : $pageCount;
           //按客户查询
           if(!empty($params['client_id'])){
               $xwhere['client_id'] = $params['client_id'];        
           }           
          
           //按发票号查询
           if(!empty($params['invoice_no'])){
               $where['invoice_no'] = $params['invoice_no'];                  
           }         
       }  
       
        $datas = $this->model
            ->where($where)           
            ->order('id', 'desc')
            ->select();
        foreach ($datas as $key => $item) {
            if(in_array($item['type'], [1,2,3,10])){
                if($item['inout']==0){
                    $income = Db::name('team_income')->where($xwhere)->where(['type'=>$item['type'],'id'=>$item['income_id']])->find();
                    $team = Db::name('team')->where(['type'=>$income['type'],'id'=>$income['parent_id']])->find();
                    $item['team_id'] = $team['id'];
                    $item['title'] = $team['title'];
                    $item['sn'] = Db::name('team')->where(['type'=>$income['type'],'id'=>$income['parent_id']])->value('sn');                   
                    $item['income'] = $income['sell_price'] * $income['num'];
                    $item['salesman'] = Db::name('manager')->where(['id'=>$income['salesman_id']])->value('cn_name');
                    $item['client'] = Db::name('client')->where(['id'=>$income['client_id']])->value('company_name');
                    $receipts = Db::name('receipt')->where(['type'=>$income['type'],'income_id'=>$income['id']])->select();
                    $total = 0;
                    foreach($receipts as $key => $value){
                        $total=$total+$value['money'];
                    }
                    $item['receipt'] = $total;
                }                
            }else{
                if($item['inout']==0){                    
                    $income = Db::name('single')->where($xwhere)->where(['type'=>$item['type'],'id'=>$item['income_id']])->find();
                    $item['title'] = $income['product_name'];
                    $item['sn'] = $income['sn'];
                    $item['income'] = ($income['sell_price'] - $income['pay_price']) * $income['num'];
                    $item['salesman'] = Db::name('manager')->where(['id'=>$income['salesman_id']])->value('cn_name');
                    $item['client'] = Db::name('client')->where(['id'=>$income['client_id']])->value('company_name');
                    $receipts = Db::name('receipt')->where(['type'=>$income['type'],'income_id'=>$income['id']])->select();
                    $total = 0;
                    foreach($receipts as $key => $value){
                        $total=$total+$value['money'];
                    }
                    $item['receipt'] = $total;
                }                
            }
        }
        $dataCount = $this->model->where($where)->count(); 
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;
    }
     //添加
     public function create(){
        $param = $this->request->param();  
        
        $params = json_decode($param['params'],true);        
        $operator_id = $param['operator_id'];    
        $data = $this->model->allowField(true)->isUpdate(false)->save($params);
        if($data){
            Logs::add("新增发票记录",$operator_id,"发票", null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }
    //更新
    public function update(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true); 
        $old = $this->model->where("id",$params['id'])->find();     
        $data = $this->model->allowField(true)->isUpdate(true)->save($params);
        if($data){
            Logs::add("修改发票记录",$operator_id,"发票", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();
        $old = $this->model->where("id",$params['id'])->find();   
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            Logs::add("删除发票记录",$params['operator_id'],"发票", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $data;
    }
}
